package com.sendtion.xrichtextdemo;

import com.bumptech.glide.Glide;
import com.sendtion.xrichtext.IImageLoader;
import com.sendtion.xrichtext.ResourceTable;
import com.sendtion.xrichtext.XRichText;
import com.sendtion.xrichtextdemo.comm.TransformationScale;

import ohos.aafwk.ability.AbilityPackage;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.app.Context;

/**
 * MyApplication
 *
 * @since 2021-06-22
 */
public class MyApplication extends AbilityPackage {
    private static final int INT_10 = 10;

    @Override
    public void onInitialize() {
        super.onInitialize();
        XRichText.getInstance().setImageLoader(new IImageLoader() {
            @Override
            public void loadImage(String imagePath, Image imageView, int imageHeight) {
                if (imageHeight > 0) {
                    ComponentContainer.LayoutConfig lp = new ComponentContainer
                            .LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, imageHeight);
                    lp.setMarginBottom(INT_10);
                    imageView.setLayoutConfig(lp);
                    getGlide(imagePath, imageView);
                } else {
                    getGlide(imagePath, imageView);
                }
            }
        });
    }

    private void getGlide(String imagePath, Image imageView) {
        Glide.with(getApplicationContext()).asBitmap().load(imagePath)
                .placeholder(ResourceTable.Media_img_load_fail)
                .error(ResourceTable.Media_img_load_fail).into(new TransformationScale(imageView));
    }
}